package lib;

import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import dao.DictionaryDAO;
import dao.TopicDAO;
import pojo.Dictionary;
import pojo.Topic;

public class TopicTableModel extends AbstractTableModel{
	public static final int TOPICID_INDEX = 0;
	public static final int TOPICNAME_INDEX = 1;
	public static final int PARENTID_INDEX = 2;
	protected String[] columnNames = {"TopicID", "TopicName", "ParentID"};
	protected Vector<Topic> dataVector;
	public TopicTableModel(){
		dataVector = new Vector<Topic>();
		TopicDAO topicdao = new TopicDAO();
		List<Topic> topic = topicdao.getList();
		dataVector.addAll(topic);
	}
	public String getColumnName(int column){
		return columnNames[column];
	}
	public Class getColumnClass(int column){
		switch(column){
		case TOPICID_INDEX:
			return Integer.class;
		case TOPICNAME_INDEX:
			return String.class;
		case PARENTID_INDEX:		
			return Integer.class;
		default:
			return Object.class;
		}
	}
	@Override
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return columnNames.length;
	}
	@Override
	public int getRowCount() {
		// TODO Auto-generated method stub
		return dataVector.size();
	}
	@Override
	public Object getValueAt(int row, int column) {
		// TODO Auto-generated method stub
		Topic topic = dataVector.get(row);
		switch(column){
		case TOPICID_INDEX:
			return topic.getTopicId();
		case TOPICNAME_INDEX:
			return topic.getTopicName();
		case PARENTID_INDEX:
			return topic.getParentId();
		default:
			return topic;	
		}
	}
	public void setValueAt(Object value, int row, int column){
		Topic topic = dataVector.get(row);
		switch(column){
		case TOPICID_INDEX:
			topic.setTopicId((Integer)value);
			break;
		case TOPICNAME_INDEX:
			topic.setTopicName((String)value);
			break;
		case PARENTID_INDEX:
			topic.setParentId((Integer)value);
			break;
		default:
			break;
		}
		fireTableCellUpdated(row, column);
	}
	public boolean isCellEditable(int row, int column){
		return false;
	}
	public boolean hasEmptyRow(){
		if(dataVector.size() == 0)
			return true;
		return false;
	}
	public void addEmptyRow(){
		dataVector.add(new Topic());
	}
}
